package com.kevin.listener;

import javax.servlet.http.HttpSession;
import javax.servlet.http.HttpSessionEvent;

import lombok.Data;

import org.directwebremoting.Browser;
import org.directwebremoting.ScriptSession;
import org.directwebremoting.ScriptSessionFilter;

import com.kevin.chat.User;
import com.kevin.chat.UserManager;

public class HttpSessionListener implements
		javax.servlet.http.HttpSessionListener {

	@Override
	public void sessionCreated(HttpSessionEvent arg0) {
		// TODO Auto-generated method stub

	}

	@Override
	public void sessionDestroyed(HttpSessionEvent arg0) {
		// TODO Auto-generated method stub
		//删除scriptsession
		final HttpSession session = arg0.getSession();
		Browser.withAllSessionsFiltered(new ScriptSessionFilter() {
			
			public boolean match(ScriptSession arg0) {
				if(arg0.getAttribute("userid").equals(session.getId())){
					arg0.invalidate();
					return true;
				}
				return false;
			}
		}, new SendMessageThread());
		UserManager.removeUser(new User(session.getId(), ""));
	}
	@Data class SendMessageThread implements Runnable{
		public SendMessageThread() {
			// TODO Auto-generated constructor stub
		}
		
		public void run() {			
		}
		
	}

}
